package com.zfy.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfy.cmsservice.entity.Classes;
import com.zfy.cmsservice.entity.vo.CascaderClassItemVo;
import com.zfy.cmsservice.entity.vo.CascaderClassVo;
import com.zfy.cmsservice.entity.vo.ClassesVo;
import com.zfy.cmsservice.mapper.ClassesMapper;
import com.zfy.cmsservice.service.ClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zfy.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-11-11
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {
    @Autowired
    private ClassesMapper classesMapper;
    @Override
    public R pageClasses(long page, long size, Integer grade) {
//        // 创建分页page对象
//        Page<Classes> classesPage = new Page<>(page, size);
//        // 构建条件Wrapper
//        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
//        if(grade!= null) {
//            wrapper.eq("grade",grade);
//        }
//        // 排序
//        wrapper.orderByDesc("gmt_modified");
//        // 调用方法实现带分页条件查询
//        this.page(classesPage, wrapper);
//        long total = classesPage.getTotal(); // 总记录数
//        List<Classes> records = classesPage.getRecords();// 数据 list 集合
//        return R.ok().data("total", total).data("content", records);
//        不走分页插件的处理
        page = (page-1)*size;
        List<ClassesVo> records = classesMapper.pageClasses(page,size,grade);
        int total = classesMapper.pageClassesTotal(page,size,grade);
        return R.ok().data("total", total).data("content", records);
    }

    @Override
    public List<CascaderClassVo> cascaderClass() {
        List<CascaderClassVo> result = new ArrayList<>();
        List<Integer> grades = classesMapper.selectDistinctGrades();
        for (Integer grade : grades) {
            CascaderClassVo cascaderClassVo = new CascaderClassVo();
            cascaderClassVo.setValue(grade);
            cascaderClassVo.setLabel(grade);
            List<CascaderClassItemVo> classes = classesMapper.selectClassesByGrade(grade);
            cascaderClassVo.setChildren(classes);
            result.add(cascaderClassVo);
        }
        return result;
    }
}
